from typing import List


class Solution:
    def countPairs(self, nums: List[int], target: int) -> int:
        nums.sort()
        ret = 0
        # left = 0, right = 0
        # example: [-1,1,2,3,5]
        for left in range(0, len(nums) - 1):
            right = len(nums) - 1
            # cut branches
            if nums[left] + nums[left + 1] >= target:
                continue
            # in close range
            while right > left:
                if nums[right] + nums[left] >= target:
                    right -= 1
                else:
                    ret += right - left
                    break
        return ret
                
